﻿/*
Copyright (C) 2008 Hemakumar M.*/
/**
  This gadget works as File Explorer for Windows OS.
*/

plugin.SetFlags(gddPluginFlagNone, gddContentFlagHaveDetails);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var currentPath = "";
var folderImg = framework.graphics.loadImage("folder.png");
var fileImg = framework.graphics.loadImage("file.png");
var driveImg = framework.graphics.loadImage("drive.png");
var parentImg = framework.graphics.loadImage("parent.png");

var tmpYcarea = carea.y;
var tmpCareaHeight = carea.height;
var tmpYdivSearch = divSearch.y;
var tmpDivSearch = divSearch.x;
var tmpBtnSearch = btnSearch.x;
var tmpEditSearch = editSearch.x;
var showingSearch = false;


carea.dropTarget = false;
careaMaxItems = 50;
carea.maxContentItems = careaMaxItems;

/**Handles the Drag Drop functionality**/
carea.ondragdrop = function() {
	var obj = event.dragFiles;

  if(!obj) 
    return;

	var e = new Enumerator(obj);	
  var size = 0;
  var filesList = "";
  while(!e.atEnd()){
    size = size + 1;
    filesList += "\n" + e.item();
    e.moveNext();
  }
  e.moveFirst();
  if(size > 1) {
   if(confirm(ARE_YOU_SURE_FILES + " " + filesList)){
     //
   }else 
			return;
  }
  
	while (!e.atEnd()) {
    var path = e.item();
    if(fso.FolderExists(path)){ 
      if(size>1 || confirm(ARE_YOU_SURE_FOLDER + " - " + path)){
				fso.MoveFolder(path, currentPath);
      }
    }else if(fso.FileExists(path)){
			if(size>1 || confirm(ARE_YOU_SURE_FILE + " - " + path)){
				fso.MoveFile(path, currentPath);
      }
		}
    e.moveNext();
  }
	var item = new ContentItem(); 
	item.heading = "";//currentPath;
	ShowFolderList(item);
}

/**On Open.. starts with displaying the Drives list from the system.**/
function view_onOpen() {
	HideSearchBar();
	
	var s, e;
	
	e = new Enumerator(fso.Drives);
	s = "";
 carea.removeAllContentItems();
	for (;!e.atEnd();e.moveNext())
	{
		x = e.item();
		s = x.DriveLetter;	
	  var item = new ContentItem();
	  item.heading = s + ":\\"; 
		item.source =  s + ":\\"; 
		item.image = driveImg;
		item.flags = gddContentItemFlagNoRemove + gddContentItemFlagLeftIcon;
    item.onDetailsView = ShowFolderList;
  	carea.addContentItem(item,  gddItemDisplayInSidebar); 
	}
  currentPath = "";
  pathLabel.innerText = MY_COMPUTER;
  carea.dropTarget = false;
	btnSearch.visible = false;
}

/**Showing the Folders and files list, upon selecting a drive/folder**/
function ShowFolderList(item) {

  carea.dropTarget = true;
	btnSearch.visible = true;
  var folder = fso.getFolder(currentPath +item.heading);

  if(folder.IsRootFolder)
		currentPath += item.heading;
  else{
			currentPath += item.heading;
      if(new String(currentPath).charAt(new String(currentPath).length -1) != '\\')
				currentPath += "\\";
      
	}
  
  pathLabel.innerText = trimPath(currentPath);
	
	carea.removeAllContentItems();
/**FILES*/
	var files = new Enumerator(folder.Files);
  var i=0;
  for (;!files.atEnd();files.moveNext(),i++){
		var item = new ContentItem();
		item.heading = files.item().Name; 
		item.source =  files.item().Name; 
		item.image = fileImg;
		item.flags = gddContentItemFlagNoRemove + gddContentItemFlagLeftIcon;
		item.onDetailsView = OpenFile;
		carea.addContentItem(item, gddItemDisplayInSidebar);	
  }
	if(i+10>=careaMaxItems)
		careaMaxItems = i+10;
/**FOLDERS*/
	var subFolders = new Enumerator(folder.SubFolders);
    for (;!subFolders.atEnd();subFolders.moveNext(), i++){
			
			var item = new ContentItem();
			item.heading = subFolders.item().name; 
			item.source =  subFolders.item().name; 
			item.image = folderImg;
			item.onDetailsView = ShowFolderList;
      //item.tooltip = "Last Modified: " + new Date(subFolders.item().DateLastModified).toLocaleString();
			item.flags = gddContentItemFlagNoRemove + gddContentItemFlagLeftIcon;
			carea.addContentItem(item, gddItemDisplayInSidebar);	
		}
	if(i+10>=careaMaxItems)
		careaMaxItems = i+10;

  carea.maxContentItems = careaMaxItems;
  var itemParent = new ContentItem();
  itemParent.heading = "../" ;
	itemParent.image = parentImg;
	itemParent.tooltip = PARENT_FOLDER;
	itemParent.flags = gddContentItemFlagNoRemove + gddContentItemFlagLeftIcon;
	itemParent.onDetailsView = ShowParentFolder;
	carea.addContentItem(itemParent, gddItemDisplayInSidebar);

}

/**Handles the Parent folder selection**/
function ShowParentFolder(parentItem) {
  var path = new String(currentPath);
  var newPath = path;

  var parentFolder = fso.getFolder(path);
  if(!parentFolder.IsRootFolder){
		path = path.substring(0,path.lastIndexOf("\\"));
		path = path.substring(0,path.lastIndexOf("\\")+1);
    
		var parentFolder2 = fso.getFolder(path);
		if(!parentFolder2.IsRootFolder){
			newPath = path.substring(path.lastIndexOf("\\")+1);
			currentPath = path.substring(0, path.lastIndexOf("\\"));
    }else{
      newPath = path;
			currentPath = "";
    }
    
		var item = new ContentItem(); 
		item.heading = newPath;
		ShowFolderList(item);

  }else{
		view_onOpen();
  }

}
/**File Open functioality*/
function OpenFile(item) {
	if(confirm(ARE_YOU_SURE_OPEN_FILE)) {
  
		var oShell = new ActiveXObject("Shell.Application");
		oShell.ShellExecute(currentPath + item.heading, "", "", "open", "1");
  }
}
/**String utility.. for trimming the path to display on the top**/
function trimPath(path) {

	var pathStr = new String(path);
	if(pathStr.length - 15 > 0)
		return   ".." + pathStr.substring(pathStr.length-15);
	else
    return pathStr;
}

/**Handles search button display and animation functionality**/
function btnSearch_onclick() {

  showingSearch = !showingSearch;
  if(showingSearch){
		pullDownSearchBar();
    editSearch.focus();
  }else{
		//HideSearchBar(); //with out animation
		pushUpSearchBar(); //with animation effect
		// Show the Complete list
		if(currentPath.length>0){
			var item = new ContentItem(); 
			item.heading = "";
			ShowFolderList(item);
		}else{
			view_onOpen();
		}
	}
}
/*Pulls the search bar down.. with animation effect*/
function pullDownSearchBar() {
		view.beginAnimation(function() { carea.y = tmpYcarea + event.value; carea.height = tmpCareaHeight - event.value; divSearch.y = tmpYdivSearch + event.value; }, // callback
                      0, 
                      22, 
                      300); 	
    divSearch.background="searchbar.png";
		editSearch.visible = true;
    view.beginAnimation(function() { divSearch.x = event.value; btnSearch.x= (150-event.value); },
          150, 
          8,
          300);
		view.beginAnimation(function() { editSearch.x = event.value; },
          25, 
          12,
          50);
}
/*Pushes back(hides) the search bar up.. with animation effect*/
function pushUpSearchBar() {
	view.beginAnimation(function() { editSearch.x = event.value; if(event.value==25) {editSearch.visible=false; editSearch.value="";}},
          12, 
          25,
          500);
	view.beginAnimation(function() { divSearch.x = event.value; btnSearch.x = (150-event.value); if(event.value==150) divSearch.background=""; },
          8, 
          150,
          500);
  view.beginAnimation(function() { carea.y = tmpYcarea - event.value; carea.height = tmpCareaHeight + event.value; divSearch.y = tmpYdivSearch + event.value; }, // callback
                      22, 
                      0, 
                      500); 
}
/**Hides the search bar (Not using)*/
function HideSearchBar() {
		carea.y=tmpYcarea;
		carea.height = tmpCareaHeight;
    divSearch.y=tmpYdivSearch;
    btnSearch.x = tmpBtnSearch;
    divSearch.x = tmpDivSearch;
		editSearch.x = tmpEditSearch;
    divSearch.background="";
		editSearch.visible = false;
		editSearch.value="";
}

/*-----FILTER---Functionality-----------*/
var FileName = new String();
var Extention = new String();

function Filter()
{
	FileName = (editSearch.value.lastIndexOf(".")>-1)? editSearch.value.slice(0,editSearch.value.lastIndexOf(".")):(editSearch.value.length>0)? editSearch.value.toLowerCase():"*"; //Get Searched File Name
	Extention = (editSearch.value.lastIndexOf(".")>-1)? editSearch.value.slice(editSearch.value.lastIndexOf(".")+1).toLowerCase():"*"; // Get Searched File Extention Name
  var path = currentPath;
	if(path.length>0 && fso.FolderExists(path)){
		
    carea.removeAllContentItems();
    
		var folder = fso.GetFolder(path);
		var filesEnum = new Enumerator(folder.Files);
		for(i=0;!filesEnum.atEnd();filesEnum.moveNext())
		{
		if(FileName == "*" ||  filesEnum.item().name.slice(0,filesEnum.item().name.lastIndexOf(".")).toLowerCase().indexOf(FileName)>-1)
			if(Extention == "*" || filesEnum.item().name.slice(filesEnum.item().name.lastIndexOf(".")+1).toLowerCase().indexOf(Extention)>-1){
				var item = new ContentItem();
				item.heading = filesEnum.item().Name; 
				item.source =  filesEnum.item().Name; 
				item.image = fileImg;
				item.flags = gddContentItemFlagNoRemove + gddContentItemFlagLeftIcon;
				item.onDetailsView = OpenFile;
				carea.addContentItem(item, gddItemDisplayInSidebar);	
				i++
			}
		}
		if(i+10>=careaMaxItems)
			careaMaxItems = i+10;

	}
	else alert(INCORRECT_PATH);
}
function editSearch_onkeypress() {
	Filter();
}

function btnhome_onclick() {
  
	view_onOpen();

}
